/**
*AUTHOR:第七页(diqye) 2014年8月27日,  13:12:22 QQ:262666212
*使用例子
*均速度运动
* effect({speed:1/1000,start:0,end:100},function(val,interval){dom.style.left=val+"px"});
*反弹运动
*effect.f2(0,100,function(val){
                    dom.style.top=val+"px";
                });
**/
(function(diqye,factory){
	if (typeof exports === 'object') {
        // Node. 
        module.exports = factory.call(diqye,require("./ticker.js"));
    } else if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(["diqye/animation/ticker"],function(ticker){ return factory.call(diqye,ticker) });
    } else {
        // Browser globals (diqye is window)
        window.effect = factory.call(diqye,ticker);
    }
}(this,function(ticker){
     //直线匀速运动
     function f1(range,callback){
        var currTime = new Date().getTime(),val,valSum=range.start;
        range.changeSpeed=range.changeSpeed||function(speed){return speed;};
        ticker.subscribe(function(time,interval){
            if(range.end<range.start){
                range.speed = range.speed<0?range.speed:range.speed*-1;
            }
            val=range.speed*interval;
            valSum+=val;
            range.speed=range.changeSpeed(range.speed,valSum,interval);
            if(range.end<range.start?valSum>=range.end:valSum<=range.end){
                callback(valSum,interval);
            }else{
                callback(range.end,interval);
                return "END";
            }
            
        });
        ticker.start();

     }

     //简单模拟碰撞效果
     function f2(start,end,callback,baseSpeed){
        var baseSpeed = baseSpeed||1/1000
        ,speed=baseSpeed
        ,dir="down"
        ,valSum=start
        ,upend=0
        ,downSum=0;
        ticker.subscribe(function(time,interval){
          if(dir=="down"){
             valSum+=interval*speed;
             speed+=baseSpeed*interval;
             if(valSum>=end){
                valSum=end;
                downSum++;
                dir="up";
             }
             callback(valSum,interval);
          }else if(dir=="up"){
            if(speed<=0.00001){
                callback(end,interval);
                return "END";
            }
            speed-=baseSpeed*interval*2;
            
            if(speed<=0){
                dir="down";
            }
            valSum-=interval*speed;
             callback(valSum,interval);
          }
          

        });
        ticker.start();
     }
     
     var fn=f1;
     fn.f2=f2;
     return fn;

}));